package com.ea.controller.front;


import com.ea.api.controller.user.CartApi;
import com.ea.common.enums.OperationResultCode;
import com.ea.common.exception.ServiceException;
import com.ea.common.response.JsonResult;
import com.ea.domain.dto.CartDTO;
import com.ea.domain.vo.CartVO;
import com.ea.service.ICartService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author NAME-L
 * @Description TODO
 * @className CartInfoController
 * @date 2022-12-06 20:45:50
 */
@RestController
@RequestMapping("/electric/front/car")
public class CartInfoController implements CartApi {

    @Autowired
    private ICartService cartService;

    @Override
    @PostMapping("/add")
    public JsonResult addCart(@RequestBody CartDTO addDTO) {
        if (addDTO == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        cartService.addCart(addDTO);
        return JsonResult.doSuccessful();
    }

    @Override
    @PostMapping("/change")
    public JsonResult updateCart(@RequestBody CartDTO addDTO) {
        if (addDTO == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        cartService.updateCart(addDTO);
        return JsonResult.updateSuccessFul();
    }

    @Override
    @PostMapping("/del")
    public JsonResult delCartGoods(@RequestParam("skuIdList") List<Long> skuIdList) {

        if (CollectionUtils.isEmpty(skuIdList)) {
            return JsonResult.delSuccessFul();
        }
        cartService.delCartGoods(skuIdList);
        return JsonResult.doSuccessful();
    }

    @Override
    @GetMapping("/del/all")
    public JsonResult delCart(@RequestParam("userId") Long userId) {
        if (userId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        cartService.delCart(userId);
        return JsonResult.delSuccessFul();
    }

    @Override
    @GetMapping("/info")
    public JsonResult showCart(@RequestParam("userId") Long userId) {
        if (userId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        List<CartVO> cartVOList = cartService.showCart(userId);
        return JsonResult.doSuccessful(cartVOList);
    }

}
